package training;

import java.util.Arrays;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_5_28_Training {
    //排布二进制网格的最少交换次数
    public static int ret = 0;
    public static int minSwaps(int[][] grid) {
        int n = grid[0].length;
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = n-1; j >= 0; j--) {
                if(grid[i][j]==0){
                    count++;
                }else {
                    break;
                }
                nums[i] = count;
            }
        }
        int[] copy = Arrays.copyOf(nums,nums.length);
        Arrays.sort(copy);
        for (int i = 0; i < n; i++) {
            if(copy[i]<i){
                return -1;
            }
        }
        int index = 0;
        for (int i = 0; i < n; i++) {
            if(nums[i]<n-1){
                index = i;
                break;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if(nums[j]<nums[j+1]){
                    chage(nums,j,j+1);
                }
            }
        }

        return ret;
    }

    public static void chage(int[] nums,int i,int j){
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
        ret++;
    }

    public static void main1(String[] args) {
        int[] nums = {2,1,4,2,35};
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void main(String[] args) {
        int[][] nums = {{0,0},{0,1}};
        System.out.println(minSwaps(nums));
    }
}



